package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.Setmeal;
import com.zrrd.reggie.bean.SetmealDish;
import com.zrrd.reggie.bean.dto.SetmealDto;
import com.zrrd.reggie.mapper.SetmealMapper;
import com.zrrd.reggie.service.SetmealDishService;
import com.zrrd.reggie.service.SetmealService;
import com.zrrd.reggie.util.CustException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    public void saveSetmealWithDish(SetmealDto dto) {
        this.save(dto);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteSetmealWithDish(Long[] ids) throws CustException {
        QueryWrapper<Setmeal> wrapper=new QueryWrapper<>();
        wrapper.eq("status",1);
        wrapper.in("id",ids);
        int count=this.count(wrapper);
        if(count>0){
            throw new CustException("不能删除正在售卖的商品");
        }
        this.removeByIds(Arrays.asList(ids));
        QueryWrapper<SetmealDish> dishQueryWrapper=new QueryWrapper<>();
        dishQueryWrapper.in("setmeal_id",ids);
        setmealDishService.remove(dishQueryWrapper);
    }
    /*
    @Override
    @Transactional
    public boolean deleteSetmealWithDish(Long[] ids) {
        boolean a=false;
        boolean b=false;
        for (Long id : ids) {
            Setmeal byId = this.getById(id);
            if(byId.getStatus()==1) {
                return false;
            }
        }
        for (Long id : ids) {
            a = this.removeById(id);
        }
        for (Long id : ids) {
            b=setmealDishService.remove(new QueryWrapper<SetmealDish>().eq("setmeal_id",id));
        }
        return a&b;
    }
     */

    @Override
    public SetmealDto getSetmealWithDish(Long id) {
        Setmeal setmeal=this.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        QueryWrapper<SetmealDish> wrapper=new QueryWrapper<>();
        wrapper.eq("setmeal_id",id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    @Transactional
    public boolean updateSetmealWithDish(SetmealDto dto) {
        //对两张表进行修改
        this.updateById(dto);
        //删除setmealdish表中相应数据
        QueryWrapper<SetmealDish> wrapper=new QueryWrapper<>();
        wrapper.eq("setmeal_id",dto.getId());
        setmealDishService.remove(wrapper);

        List<SetmealDish> dishList=dto.getSetmealDishes();
        for (SetmealDish setmealDish : dishList) {
            //加数据 传过来的数据只要给setmealid赋值就行
            setmealDish.setSetmealId(dto.getId());
        }
        return setmealDishService.saveBatch(dishList);
    }
}
